package com.yjz.study.adapter.detail

import android.content.Context
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextUtils
import android.text.style.BackgroundColorSpan
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yjz.common.extend.loadCircle
import com.yjz.study.R
import com.yjz.study.adapter.home.GoodsItem
import com.yjz.study.http.entity.res.GoodsDetailModel
import com.yjz.study.http.entity.res.GoodsModel
import com.yjz.ui.recycler.item.HiDataItem
import com.yjz.ui.R.color
import com.yjz.ui.recycler.item.HiAdapter
import com.yjz.ui.recycler.item.HiViewHolder

/**
 *作用 : 商品详情 店铺详情
 *作者 : yjz
 *时间 : 2025/5/15 10:30
 */
class GoodsDetailShopInfoItem(private val goodsDetailModel: GoodsDetailModel) : HiDataItem<GoodsDetailModel, HiViewHolder>() {

    private val SPAN_FLOW_COUNT = 3

    override fun onBindData(holder: HiViewHolder, position: Int) {
        val context = holder.itemView.context ?: return

        val shopLogo = holder.itemView.findViewById<ImageView>(R.id.shop_logo)
        val shopTitle = holder.itemView.findViewById<TextView>(R.id.shop_title)
        val shopDes = holder.itemView.findViewById<TextView>(R.id.shop_des)

        val shop = goodsDetailModel.shop

        // 店铺简介
        shop?.let {
            shopLogo.loadCircle(it.logo)
            shopTitle.text = it.name
            shopDes.text = it.evaluation

            val evaluation: String? = shop.evaluation
            val tagContainer = holder.itemView.findViewById<LinearLayout>(R.id.shop_tags)
            if (TextUtils.isEmpty(evaluation)) {
                tagContainer.visibility = View.GONE
            } else {
                tagContainer.visibility = View.VISIBLE
                //6个元素 每2个组成一个标签  "evaluation":"描述相符 高 服务态度 高 物流服务 高"
                val contains = evaluation?.contains(" ")
                if (true == contains) {
                    var index = 0
                    val serviceTags = evaluation.split(" ")
                    for (tagIndex in 0..<serviceTags.size / 2) {
                        val tagView: TextView = if (tagIndex < tagContainer.childCount - 1) {
                            tagContainer.getChildAt(tagIndex) as TextView
                        } else {
                            val tagView = TextView(context)
                            val params =
                                LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT)
                            params.weight = 1f
                            tagView.layoutParams = params
                            tagView.gravity = Gravity.CENTER
                            tagView.textSize = 14f
                            tagView.setTextColor(ContextCompat.getColor(context, color.color_666))
                            tagContainer.addView(tagView)
                            tagView
                        }
                        val serviceName = if (index >= serviceTags.size) continue else serviceTags[index]
                        val serviceTag = serviceTags[index + 1]
                        index += 2
                        val spanTag = spanServiceTag(context, serviceName, serviceTag)
                        tagView.text = spanTag
                    }
                }
            }
        }

        // 商品
        val flowRecycler = holder.itemView.findViewById<RecyclerView>(R.id.flow_recycler)
        val flowGoods = goodsDetailModel.flowGoods
        if (flowGoods.isNullOrEmpty()) {
            flowRecycler.visibility = View.GONE
        } else {
            flowRecycler.visibility = View.VISIBLE

            if (flowRecycler.layoutManager == null) {
                flowRecycler.layoutManager = GridLayoutManager(context, SPAN_FLOW_COUNT)
            }

            if (flowRecycler.adapter == null) {
                flowRecycler.adapter = HiAdapter(context)
            }

            val dataItems = mutableListOf<GoodsItem>()
            flowGoods.forEach {
                dataItems.add(ShopGoodsItem(it))
            }
            val adapter = flowRecycler.adapter as HiAdapter
            adapter.clearItems()
            adapter.addItems(dataItems, true)
        }
    }

    inner class ShopGoodsItem(goodsModel: GoodsModel) : GoodsItem(goodsModel, false) {

        override fun getItemLayoutRes(): Int {
            return R.layout.layout_goods_detail_shop_info_item_goods
        }

        override fun onViewAttachedToWindow(holder: HiViewHolder) {
            super.onViewAttachedToWindow(holder)
            val viewParent: ViewGroup = holder.itemView.parent as ViewGroup
            val availableWidth =
                viewParent.measuredWidth - viewParent.paddingLeft - viewParent.paddingRight
            val itemWidth = availableWidth / SPAN_FLOW_COUNT

            val itemImage = holder.itemView.findViewById<ImageView>(R.id.item_image)
            val params = itemImage.layoutParams
            params.width = itemWidth
            params.height = itemWidth
            itemImage.layoutParams = params
        }
    }

    private fun spanServiceTag(context: Context, serviceName: String, serviceTag: String): CharSequence {
        val ssb = SpannableStringBuilder()
        ssb.append(serviceName)

        val ss = SpannableString(serviceTag)
        ss.setSpan(
            ForegroundColorSpan(ContextCompat.getColor(context, color.color_c61)),
            0,
            ss.length,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        ss.setSpan(
            BackgroundColorSpan(ContextCompat.getColor(context, color.color_f8e)),
            0,
            ss.length,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        ssb.append(ss)
        return ssb
    }

    override fun getItemLayoutRes(): Int {
        return R.layout.layout_goods_detail_shop_info_item
    }
}